package com.mayjean.gateway.props;

import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties;
import com.mayjean.gateway.listener.DynamicGatewayRouteDefinitionListener;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.cloud.gateway.route.RouteDefinition;

import java.util.List;

/**
 * nezha gateway properties
 *
 * @author kyleju
 */
@Data
@ConfigurationProperties("nezha.gateway")
@NacosConfigurationProperties(prefix = "nezha.gateway", dataId = NezhaGatewayProperties.DATA_ID, groupId = NezhaGatewayProperties.GATEWAY_GROUP_ID, type = ConfigType.YAML)
@RequiredArgsConstructor
public class NezhaGatewayProperties implements InitializingBean {

	
	public static final String DATA_ID = "${nezha.gateway.dataId:nezha-gateway-config}";
	public static final String GATEWAY_GROUP_ID = "${nezha.gateway.groupId:DEFAULT_GROUP}";
	private final ObjectProvider<DynamicGatewayRouteDefinitionListener> dynamicGatewayPropertiesListener;
	
	/**
	 * routes {@link List< RouteDefinition >}
	 */
	@NestedConfigurationProperty
	private List<RouteDefinition> routes;

	@Override
	public void afterPropertiesSet() throws Exception {
		if (dynamicGatewayPropertiesListener != null) {
			dynamicGatewayPropertiesListener.ifAvailable(listener -> {
				listener.onChange(this);
			});
		}
	}
}